package com.xuecheng.content.api;

import com.xuecheng.base.exception.BusinessException;
import com.xuecheng.base.exception.CommonError;
import com.xuecheng.content.model.po.CourseTeacher;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.content.service.CourseTeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * <p>
 * 课程-教师关系表 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
@RequestMapping("/courseTeacher")
public class CourseTeacherController {

    @Autowired
    private CourseTeacherService  courseTeacherService;

    /**
     * 获取课程教师列表
     * @param courseId
     * @return
     */
    @GetMapping("/list/{courseId}")
    public List<CourseTeacher> getTeacherListByCourseId(@PathVariable("courseId") long courseId){
        if (courseId <= 0) {
            BusinessException.cast(CommonError.PARAMS_ERROR);
        }
        return courseTeacherService.getTeacherListByCourseId(courseId);
    }


    /**
     * 添加教师到课程
     * @param courseTeacher
     * @return
     */
    @PostMapping
    public CourseTeacher addTeacherToCourse(@RequestBody CourseTeacher courseTeacher){
        if (courseTeacher == null) {
            BusinessException.cast(CommonError.REQUEST_NULL);
        }
        return courseTeacherService.addTeacherToCourse(courseTeacher);
    }


    /**
     * 修改教师信息
     * @param courseTeacher
     * @return
     */
    @PutMapping
    public CourseTeacher updateTeacher(@RequestBody CourseTeacher courseTeacher){
        if (courseTeacher == null) {
            BusinessException.cast(CommonError.REQUEST_NULL);
        }
        return courseTeacherService.updateTeacherInfo(courseTeacher);
    }

    /**
     * 删除教师信息
     * @param
     * @return
     */
    @DeleteMapping("/course/{courseId}/{teacherId}")
    public void deleteTeacher(@PathVariable("courseId") long courseId,@PathVariable("teacherId")long teacherId){
        if (courseId <= 0 || teacherId <= 0) {
            BusinessException.cast(CommonError.PARAMS_ERROR);
        }
        courseTeacherService.deleteTeacher(courseId,teacherId);
    }
}
